#include "base_lift.h"

namespace application {
namespace lift {

BaseLift::BaseLift(int liftNum, LiftType type, std::shared_ptr<kernel::ILogger> logger, QObject* parent)
    : QObject(parent)
    , m_liftNum(liftNum)
    , m_type(type)
    , m_logger(logger)
{
}

void BaseLift::addFloorInfo(const FloorInfo& floorInfo)
{
    m_floorInfos.append(floorInfo);
    if (m_logger) {
        m_logger->debug(QString("电梯 %1 添加楼层信息: 楼层=%2")
            .arg(m_liftNum).arg(floorInfo.floor));
    }
}

FloorInfo BaseLift::getFloorInfo(int floor) const
{
    for (const auto& info : m_floorInfos) {
        if (info.floor == floor) {
            return info;
        }
    }
    return FloorInfo();
}

bool BaseLift::updateFloorAreaInfo(int floor, const QString& askAreaId, const QString& waitAreaId)
{
    bool updated = false;
    for (auto& info : m_floorInfos) {
        if (info.floor != floor) {
            continue;
        }

        if (!askAreaId.isEmpty() && info.askAreaId != askAreaId) {
            info.askAreaId = askAreaId;
            updated = true;
        }

        if (!waitAreaId.isEmpty() && info.waitAreaId != waitAreaId) {
            info.waitAreaId = waitAreaId;
            updated = true;
        }

        if (updated && m_logger) {
            m_logger->debug(QString("电梯 %1 更新楼层 %2 的区域绑定 (ask=%3, wait=%4)")
                .arg(m_liftNum)
                .arg(floor)
                .arg(info.askAreaId.isEmpty() ? QStringLiteral("<none>") : info.askAreaId)
                .arg(info.waitAreaId.isEmpty() ? QStringLiteral("<none>") : info.waitAreaId));
        }

        return updated;
    }

    return updated;
}

void BaseLift::setStatus(LiftStatus status)
{
    if (m_status != status) {
        m_status = status;
        if (m_logger) {
            m_logger->debug(QString("电梯 %1 状态变更: %2").arg(m_liftNum).arg(static_cast<int>(status)));
        }
        emit statusChanged(status);
    }
}

void BaseLift::setStage(LiftStage stage)
{
    if (m_stage != stage) {
        m_stage = stage;
        if (m_logger) {
            m_logger->debug(QString("电梯 %1 阶段变更: %2").arg(m_liftNum).arg(static_cast<int>(stage)));
        }
        emit stageChanged(stage);
    }
}

} // namespace lift
} // namespace application

#include "base_lift.moc"
